package org.example.lease.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.lease.model.entities.AttrKey;
import org.example.lease.model.entities.AttrValue;
import org.example.lease.model.result.Result;
import org.example.lease.service.AttrKeyService;
import org.example.lease.service.AttrValueService;
import org.example.lease.vo.attr.AttrKeyVo;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 宋孟林
 * @description
 * @date: 2025/5/16 8:13
 */
@RestController
@Slf4j
@Tag(name = "房间属性管理")
@RequestMapping("/admin/attr")
public class AttrController {
    @Resource
    private AttrValueService attrValueService;
    @Resource
    private AttrKeyService attrKeyService;

    @Operation(summary = "保存或者更新属性名")
    @PostMapping("/key/saveOrUpdate")
    public Result<?> saveOrUpdateAttrKey(@RequestBody AttrKey attrKey){
        attrKeyService.saveOrUpdate(attrKey);
        return Result.ok();
    }

    @Operation(summary = "保存或者更新属性值")
    @PostMapping("/value/saveOrUpdate")
    public Result<?> saveOrUpdateAttrValue(@RequestBody AttrValue attrValue){
        attrValueService.saveOrUpdate(attrValue);
        return Result.ok();
    }

    @Operation(summary = "查看全部属性名和属性值列表")
    @GetMapping("/list")
    public Result<List<AttrKeyVo>> listAttrInfo(){
        List<AttrKeyVo> attrKeyVos = attrKeyService.listAttrInfo();
        return Result.ok(attrKeyVos);
    }

    /**
     * 由于存在属性值和属性名两张表
     * 删除属性的时候涉及到两张表的业务关联
     * 这里的业务是删除属性名的时候，也删除属性表中的相同属性名的属性值
     * @param id id
     * @return result
     */
    @Operation(summary = "根据id删除属性名")
    @DeleteMapping("/key/deleteById")
    public Result<?> removeAttrKeyById(@RequestParam Long id){
        attrKeyService.removeById(id);
        //同时删除属性值表中的相同属性名的记录
        attrValueService.remove(Wrappers.lambdaUpdate(AttrValue.class).eq(AttrValue::getAttrKeyId, id));
        return Result.ok();
    }

    @Operation(summary = "根据id删除属性值")
    @DeleteMapping("/value/deleteById")
    public Result<?> removeAttrValueById(@RequestParam Long id){
        attrValueService.removeById(id);
        return Result.ok();
    }


}
